<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 图标区域 -->
      <div class="avatar_box"></div>

      <!-- 登录表单区域 -->
      <el-form :model="loginForm" ref="loginForm" class="login_form" :rules="loginFormRules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="username" prefix-icon="el-icon-user"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="password"
            prefix-icon="el-icon-lock"
            type="password"
            show-password
          ></el-input>
        </el-form-item>

        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="login('loginForm')">Login</el-button>
          <el-button type="info" @click="reset('loginForm')">Reset</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "network/login/login";
export default {
  data() {
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      // 表单的验证规则对象
      loginFormRules: {
        username: [
          {
            required: true,
            message: "Please enter the user name",
            trigger: "blur",
          },
          {
            min: 3,
            max: 10,
            message: "The length is between 3 and 10 characters",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "Please enter the password",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let { data: res, meta } = await login(
            this.loginForm.username,
            this.loginForm.password
          );
          if (meta.status === 400) {
            this.$message({
              message: 'Login failled',
              type: 'warning'
            })
          } else if (meta.status === 200) {
            // 登录后操作
            // 1. 登录后将token放到了vue状态管理中，（不建议）
            this.$store.dispatch("SetToken", res.token).catch((err) => {
              console.log(err);
            });
            
            //2. 将token保存到客户端的 sessionStorage中
            //  2.1 为什么不保存到localStorage中，因为localStorage是用来本地持久化的，sessionStorage是会话持久化的
            //  2.2 token只应在当前网站打开期间生效，所以将token保存在sessionStorage中
            //3. 通过编程式导航跳转到主页，路由地址/home
            sessionStorage.setItem('token',res.token)
            this.$router.replace("/home");
          } else {
            this.$message({
              message: 'Please contact your system administrator',
              type: 'error'
            })
          }
        }
      });
    },
    reset(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  .login_box {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    width: 450px;
    height: 300px;

    background-color: #fff;
    border-radius: 3px;

    .avatar_box {
      position: fixed;
      left: 50%;
      transform: translate(-50%, -50%);

      width: 100px;
      height: 100px;

      border: 8px solid #fcfdfb;
      border-radius: 50%;
      box-shadow: 0 0 10px #edeeec;

      background-color: #edeeec;
      background-image: url("~assets/logo.png");
      background-size: contain;
    }

    .login_form {
      position: absolute;
      bottom: 0;
      left: 5%;
      width: 90%;
      box-sizing: border-box;

      .btns {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}

.el-input__inner::placeholder {
  color: palevioletred;
}
</style>